 

package com.zdkj.subway.stm.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.entity.SwStTicketsInventoryAbnormalNotice;
import com.zdkj.subway.stm.service.SwStTicketsInventoryAbnormalNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


/**
 * 车票库存异常通知表
 *
 * @author songtuo
 * @date 2020-07-14 11:49:24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstticketsinventoryabnormalnotice" )
@Api(value = "swstticketsinventoryabnormalnotice", tags = "车票库存异常通知表管理")
@Slf4j
public class SwStTicketsInventoryAbnormalNoticeController {

    private final  SwStTicketsInventoryAbnormalNoticeService swStTicketsInventoryAbnormalNoticeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param swStTicketsInventoryAbnormalNotice 车票库存异常通知表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwStTicketsInventoryAbnormalNoticePage(Page page, SwStTicketsInventoryAbnormalNotice swStTicketsInventoryAbnormalNotice) {
        PigxUser user = SecurityUtils.getUser();
        swStTicketsInventoryAbnormalNotice.setLineCode(user.getLineCode());
        swStTicketsInventoryAbnormalNotice.setStationCode(user.getStationCode());
        return R.ok(swStTicketsInventoryAbnormalNoticeService.page(page, Wrappers.query(swStTicketsInventoryAbnormalNotice)));
    }


    /**
     * 通过id查询车票库存异常通知表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(swStTicketsInventoryAbnormalNoticeService.getById(id));
    }

    /**
     * 新增车票库存异常通知表
     * @param swStTicketsInventoryAbnormalNotice 车票库存异常通知表
     * @return R
     */
    @ApiOperation(value = "新增车票库存异常通知表", notes = "新增车票库存异常通知表")
    @SysLog("新增车票库存异常通知表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('swstticketsinventoryabnormalnotice_add')" )
    public R save(@RequestBody SwStTicketsInventoryAbnormalNotice swStTicketsInventoryAbnormalNotice) {
        return R.ok(swStTicketsInventoryAbnormalNoticeService.save(swStTicketsInventoryAbnormalNotice));
    }

    /**
     * 修改车票库存异常通知表
     * @param swStTicketsInventoryAbnormalNotice 车票库存异常通知表
     * @return R
     */
    @ApiOperation(value = "修改车票库存异常通知表", notes = "修改车票库存异常通知表")
    @SysLog("修改车票库存异常通知表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('swstticketsinventoryabnormalnotice_edit')" )
    public R updateById(@RequestBody SwStTicketsInventoryAbnormalNotice swStTicketsInventoryAbnormalNotice) {
        return R.ok(swStTicketsInventoryAbnormalNoticeService.updateById(swStTicketsInventoryAbnormalNotice));
    }

    /**
     * 通过id删除车票库存异常通知表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除车票库存异常通知表", notes = "通过id删除车票库存异常通知表")
    @SysLog("通过id删除车票库存异常通知表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('swstticketsinventoryabnormalnotice_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(swStTicketsInventoryAbnormalNoticeService.removeById(id));
    }

    @ApiOperation(value = "导出车票库存异常通知", notes = "导出车票库存异常通知")
    @GetMapping("/export")
    public void export(Page page, SwStTicketsInventoryAbnormalNotice notice, HttpServletResponse response) {
        List<SwStTicketsInventoryAbnormalNotice> records = swStTicketsInventoryAbnormalNoticeService.page(page, Wrappers.query(notice)).getRecords();
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("车票库存异常通知", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");
            EasyExcel.write(response.getOutputStream(), SwStTicketsInventoryAbnormalNotice.class).
                    sheet("车票库存异常通知").doWrite(records);
        } catch (IOException e) {
            log.error("导出车票库存异常通知");
        }
    }

}
